using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

// RPGGame.cs
namespace RTS_RPG
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class RPGGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public List<Texture2D> images;
        public List<Area> areas;
        //public List<Site> sites;
        public List<GameMap> gameMaps;
        public PlayerStats currentPlayer;
        public SplashScreen startScreen;
        public Overworld world;

        // public Battle currentBattle;
        public SpriteFont font { get; private set; }

        private Boolean map_mode;
        private bool splashScreen;

        public RPGGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            //Program.SCREENWIDTH = Window.ClientBounds.Width;
            //Program.SCREENHEIGHT = Window.ClientBounds.Height;
            graphics.PreferredBackBufferWidth = Program.SCREENWIDTH;
            graphics.PreferredBackBufferHeight = Program.SCREENHEIGHT;
            //graphics.IsFullScreen = true;
            //content = new ContentManager(Services);
            //Window.AllowUserResizing = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            images = new List<Texture2D>();
            areas = new List<Area>();
            //sites = new List<Site>();
            gameMaps = new List<GameMap>();
            //currentBattle = null;
            map_mode = false;
            splashScreen = true;
            base.Initialize();

            /**** The stuff below here will have to eventually be made to happen only when starting a new game. ****/
            List<Vector2> initCoors = new List<Vector2>();
            initCoors.Add(new Vector2(20, 200));
            initCoors.Add(new Vector2(10, 210));
            currentPlayer = new PlayerStats(this, null, Content);
            //areas[0] = new Area("Home Town", gameMaps[1], initCoors, new List<NPC>(), currentPlayer.party[0]);
            //sites[0] = new Site("Home Town", images[12], areas[0]);
            world = new Overworld(null, currentPlayer, graphics, Services);
            // currentPlayer.world = world;
            //currentPlayer.currentArea = areas[0];
            /*******             ********                *************                ********               *******/
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            font = Content.Load<SpriteFont>("Shared/Fonts/font1");
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            MouseState mstate = Mouse.GetState();
            KeyboardState kstate = Keyboard.GetState();
            if ((kstate.IsKeyDown(Keys.RightControl) ||
                 kstate.IsKeyDown(Keys.LeftControl)) && kstate.IsKeyDown(Keys.Q))
                this.Exit();

            if (splashScreen)
            {
                // splashScreen = hud.updateSplashScreen(kstate);
                if (kstate.IsKeyDown(Keys.Enter))
                {
                    splashScreen = false;
                    map_mode = true;
                    // currentBattle = new Battle(graphics);
                    // currentBattle.LoadContent(Content);
                }
                return;
            }

            if (map_mode)
            {
                world.update(gameTime, mstate, kstate);
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();

            if (splashScreen)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
                spriteBatch.DrawString(font, "Press 'Enter' to start...",
                    new Vector2(Program.SCREENWIDTH / 2, Program.SCREENHEIGHT / 2), Color.White);
            }

            else if (map_mode)
            {
                world.draw(gameTime, GraphicsDevice, spriteBatch);
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
